package com.songlanyun.modules.wechat.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.service.GoodsService;
import com.songlanyun.modules.user.controller.AbstractController;
import com.songlanyun.modules.wechat.entity.WechatLive;
import com.songlanyun.modules.wechat.entity.WechatLiveGoods;
import com.songlanyun.modules.wechat.service.WechatLiveGoodsService;
import com.songlanyun.modules.wechat.service.WechatLiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("admin/wechatLive")
public class WeChatLiveAdminController extends AbstractController {
    @Autowired
    private WechatLiveService wechatLiveService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private WechatLiveGoodsService wechatLiveGoodsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R page(@RequestParam Map<String, Object> params) {
        PageUtils page = wechatLiveService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R infoByShopId(@PathVariable("id") Long id) {
        WechatLive wechatLive = wechatLiveService.getById(id);
        if (wechatLive == null) {
            return R.error("参数错误");
        }
        String goodsIds = wechatLive.getWxGoodsIds();
        if (StringUtils.isNotBlank(goodsIds)) {
            List<WechatLiveGoods> goodsList = new ArrayList<>();
            LambdaQueryWrapper<WechatLiveGoods> goodsLambdaQueryWrapper;
            String[] goodsArray = goodsIds.split(",");
            for (int i = 0; i < goodsArray.length; i++) {
                WechatLiveGoods wechatLiveGoods = wechatLiveGoodsService.getById(goodsArray[i]);
                if (wechatLiveGoods != null) {
                    Goods goods = goodsService.getById(wechatLiveGoods.getProductId());
                    if (goods != null) {
                        goodsList.add(wechatLiveGoods);
                    }
                }
            }
            wechatLive.setLiveGoodsList(goodsList);
        }
        return R.ok().put("data", wechatLive);
    }


    @PostMapping("/create")
    public R create(@RequestBody WechatLive entity) {
        //未开始
        entity.setLiveStatus(102);
        WechatLive obj = wechatLiveService.create(entity);
        return R.ok().put("data", obj);
    }


    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        wechatLiveService.remove(id);
        return R.ok();
    }


    @PutMapping("/update")
    public R update(@RequestBody WechatLive entity) {
        WechatLive wechatLive = wechatLiveService.getById(entity.getRoomId());
        if (wechatLive == null) {
            return R.error("数据错误");
        }
        WechatLive wechatLiveUpdate = wechatLiveService.modify(entity);
        return R.ok().put("data", wechatLiveUpdate);
    }


    @PutMapping("/addProduct")
    public R addProduct(@RequestBody WechatLive entity) {
        WechatLive wechatLive = wechatLiveService.getById(entity.getRoomId());
        if (wechatLive == null) {
            return R.error("数据错误");
        }
        wechatLiveService.addProduct(entity);
        return R.ok().put("data", wechatLive);
    }

    @PutMapping("/removeProduct")
    public R removeProductId(@RequestBody WechatLive entity) {
        WechatLive wechatLive = wechatLiveService.getById(entity.getRoomId());
        if (wechatLive == null) {
            return R.error("数据错误");
        }
        wechatLiveService.removeProduct(entity);
        return R.ok().put("data", wechatLive);
    }


    /**
     * 信息
     */
    @RequestMapping("/goods/{id}")
    public R goods(@PathVariable("id") Long id) {
        WechatLive wechatLive = wechatLiveService.getById(id);
        return R.ok().put("data", wechatLive);
    }


    @GetMapping("/synchro")
    public ResponseEntity<Object> synchroWxOlLive() {
        wechatLiveService.synchroWxOlLive();
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
